﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;
using PCSWpfData.Extensions;

namespace PCSWpfData
{
    public class Data
    {

        public List<Instrument> GetDbContextData()
        {
            using (var Context = new CombinedDbContext())
            {

                var instr = Context.CompleteInstruments().ToList();
                
                return instr;
            }
        }
        private int pricesereiesId;
        public List<Instrument> GetData()
        {
            pricesereiesId = 0;
            var instrumentList = new List<Instrument>();
            var random = new Random();
            for (int i = 0 ; i < 100; i++)
            {
                var instrument = new Instrument();
                var name = randomName(random);
                instrument.Name = name.ToString();
                instrument.Id = i;
                instrument.InstrumentTypeId = random.Next(3);
                instrument.Active = random.Next(5) != 1;
                instrument.Special = random.Next(4) == 1;
                GetPriceSeriesList(instrument.PriceSeriesList, random, i);
                instrumentList.Add(instrument);
                    
            }

            //using (var ctx = new CombinedDbContext())
            //{

            //    instrumentList.ForEach(x => ctx.Instruments.Add(x));
            //    ctx.SaveChanges();
            //}
        

            return instrumentList;
        }

        private void GetPriceSeriesList(ICollection<PriceSeries> priceSeriesList, Random random, int instrumentId)
        {

            for (int i = 0; i <= instrumentId % 4; i++)
            {
                var priceSeries = new PriceSeries();
                priceSeries.Name = "Class " + randomName(random);
                priceSeries.Id = pricesereiesId++;
                priceSeries.InstrumentId = instrumentId;
                priceSeries.IsMaster = i == 0 ;
                priceSeries.TrackRecordType = random.Next(2);
                GetPriceSeriesPrices(random, priceSeries);
                GetPriceSeriesDividends(random, priceSeries);
                GetPriceSeriesStatistics(random, priceSeries);
                priceSeriesList.Add(priceSeries);
            }
        }

        private void GetPriceSeriesPrices(Random random, PriceSeries priceSeries)
        {
            DateTime date = new DateTime(2013,1,1);
            var firstPrice = new Price{Currency = "USD", Nav= 1000, PriceQuality=1, PriceSeriesId = priceSeries.Id,Ror = 0, ValueAsOf = date};

            var nav = firstPrice.Nav;
            for (int i = 0; i<= priceSeries.Id % 10; i++)
            {

                date = date.AddMonths(1);
                
                var price = new Price();
                price.ValueAsOf = date;
                price.Ror = random.NextDouble();
                nav = (1 + price.Ror) * nav;
                price.Nav = nav;
                price.PriceQuality= i;
                price.PriceSeriesId = priceSeries.Id;
                priceSeries.PriceList.Add(price);
            }
        }

        private void GetPriceSeriesDividends(Random random, PriceSeries priceSeries)
        {
            DateTime date = new DateTime(2013, 1, 1);
            
            for (int i = 0; i <= priceSeries.Id % 5; i++)
            {

                date = date.AddMonths(1);

                var dividend = new Dividend();
                dividend.ValueAsOf = date;
                dividend.Ror = random.NextDouble();
                dividend.PriceSeriesId = priceSeries.Id;
                priceSeries.DividendList.Add(dividend);
            }
        }

        private void GetPriceSeriesStatistics(Random random, PriceSeries priceSeries)
        {
            

           //  if (priceSeries.Id % 5 > 4)
            {
                priceSeries.Statistic = new Statictics();
                priceSeries.Statistic.Average = random.NextDouble();
                priceSeries.Statistic.Count = random.Next(1000);
                priceSeries.Statistic.Proportions = random.NextDouble();
                priceSeries.Statistic.Sum = random.NextDouble()*random.Next(10000);
            }
        }


        private static StringBuilder randomName(Random random)
        {
            var name = new StringBuilder(16);
            for (int j = 0; j < 15; j++)
            {
                int val = random.Next() % 26;
                char letter = (char)('a' + val);
                name.Append(letter);
            }
            return name;
        }
        
    }
}
